package sk.umb.nadrz.grafika;

import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;

import sk.umb.nadrz.mc.MonteCarlo;
import sk.umb.nadrz.model.BoundingBox;
import sk.umb.nadrz.model.Nadrz;


public class MainWindow extends JFrame implements WindowListener, ActionListener{
	Image background;
	
	Color back = new Color(0x49,0x42, 0x3f);
	Color water = new Color(0x00, 0xa9, 0x9d);
	Color tank = new Color(0xf1, 0xf1, 0xf1);
	Color fontColor = Color.white;
	
	Font font;
	
	String defaultText = "5.5";
	int defaultHeight = 20;
	int defaultWidth = 26;
	IntegerTextField L;
	IntegerTextField H;
	IntegerTextField A;
	IntegerTextField R;
	IntegerTextField l;
	IntegerTextField alpha;
	IntegerTextField result;
	IntegerTextField freeSpace;
	
	JButton bClear;
	JButton bOK;
	public void zobraz() throws IOException{
		  UIManager.put("TextField.border", "EmptyBorder");
		  UIManager.put("Button.select", water);
		  
		setSize(800, 620);
		setLayout(null);
		
		
		Canvas obr = new CanvasClass();
		obr.setBackground(back);
		obr.setBounds(0, 0, 800, 600);
		
		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
		try {
			ge.registerFont(Font.createFont(Font.TRUETYPE_FONT, new File("Aller_Rg.ttf")));
		} catch (FontFormatException e) {
			
			e.printStackTrace();
		} 
//		String[] s = ge.getAvailableFontFamilyNames();
//		for(int i=0; i<s.length;i++){
//			System.out.println(s[i]);
//		}
//		font = Font.decode("cinnamon cake");
		font = new Font ("Aller", Font.PLAIN, 15);
		Font fontItalic = new Font("Aller", Font.ITALIC, 15);
		Font fontBold = new Font("Aller", Font.BOLD, 15);
		
		L = new IntegerTextField();
		L.setBounds(316, 433, defaultWidth, defaultHeight);
		L.setBackground(back);
		L.setFont(font);
		L.setForeground(fontColor);
		L.setText(defaultText);
		L.setHorizontalAlignment(JTextField.CENTER);
		
		
		
		JPanel test = new JPanel();
//		test.setBounds(L.getX()-20, L.getY()-5, 20, defaultHeight);
//		test.setBackground(back);
//		JLabel Lfront = new JLabel();
//		Lfront.setBackground(back);
//		Lfront.setForeground(fontColor);
//		Lfront.setFont(fontItalic);
//		Lfront.setText("L=");
//		test.add(Lfront);
		
		
		A = new IntegerTextField();
		A.setBounds(464-defaultWidth, 270, defaultWidth, defaultHeight);
		A.setBackground(tank);
		A.setFont(font);
		A.setText(defaultText);
		A.setHorizontalAlignment(JTextField.RIGHT);
		
		H = new IntegerTextField();
		H.setBounds(235, 354, defaultWidth, defaultHeight);
		H.setBackground(water);
		H.setForeground(fontColor);
		H.setFont(font);
		H.setText(defaultText);
		
		R = new IntegerTextField();
		R.setBounds(570, 255, defaultWidth, defaultHeight);
		R.setBackground(tank);
		R.setFont(font);
		R.setText(defaultText);
		R.setHorizontalAlignment(JTextField.RIGHT);
		
		alpha = new IntegerTextField();
		alpha.setBounds(668, 378, defaultWidth, defaultHeight);
		alpha.setFont(font);
		alpha.setForeground(fontColor);
		alpha.setText(defaultText);
		alpha.setBackground(back);
		alpha.setHorizontalAlignment(JTextField.CENTER);
		
		l = new IntegerTextField();
		l.setBounds(150, 476, defaultWidth, defaultHeight);
		l.setFont(font);
		l.setForeground(fontColor);
		l.setText(defaultText);
		l.setBackground(back);
		l.setHorizontalAlignment(JTextField.CENTER);
//		form.setCursor(Cursor.getPredefinedCursor(Cursor.TEXT_CURSOR));
//		dlzka2.setBackground(txt);
		
		result = new IntegerTextField();
		result.setBounds(127, 510, 441, defaultHeight);
		result.setFont(font);
		result.setForeground(fontColor);
		result.setText("");
//		result.setText("V nadrzi je: _________ litrov");
		result.setBackground(back);
		result.setHorizontalAlignment(JTextField.LEFT);
		
		freeSpace = new IntegerTextField();
		freeSpace.setBounds(127, 530, 441, defaultHeight);
		freeSpace.setFont(font);
		freeSpace.setForeground(fontColor);
		result.setText("");
//		freeSpace.setText("V nadrzi je _____ litrov volneho miesta ");
		freeSpace.setBackground(back);
		freeSpace.setHorizontalAlignment(JTextField.LEFT);
		
		
		bOK = new JButton("Vypocet");
		bOK.setBounds(210, 570, 358, 22);
		bOK.setBackground(back);
		bOK.setForeground(fontColor);
		bOK.setBorder(new RoundedBorder(15));
		bOK.setFont(fontBold);
		bOK.addActionListener(this);
		bOK.setMnemonic(KeyEvent.VK_ENTER);
		
		
		bClear = new  JButton("Reset");
		bClear.setBounds(127, 570, 80, 22);
		bClear.setBackground(back);
		bClear.setForeground(fontColor);
		bClear.setBorder(new RoundedBorder(15));
		bClear.addActionListener(this);
		bClear.setMnemonic(KeyEvent.VK_BACK_SPACE);
		
		add(H);
		add(L);
		add(A);
		add(R);
		add(l);
		add(alpha);
		add(result);
		add(freeSpace);
		
		add(test);
		//add(Lfront);
		
		add(bOK);
		add(bClear);
		
		add(obr);
		
		
		//obr.add(dlzka);
		
		
		this.setResizable(false);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE); 
	}
	public void paintComponent(Graphics g)
	{
	g.drawImage(background, 0, 0, null);

	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(bOK)){
			try{
			Nadrz nadrz = new Nadrz(
					Double.valueOf(L.getText()), Double.valueOf(A.getText()), 
					Double.valueOf(alpha.getText()), Double.valueOf(R.getText()));
//			nadrz.setDlzka(Double.valueOf(L.getText()));
//			nadrz.setPolomer(Double.valueOf(A.getText()));
//			nadrz.setSklon(Double.valueOf(alpha.getText()));
//			nadrz.setPolomerGula(Double.valueOf(R.getText()));
			
			MonteCarlo mc = new MonteCarlo();
			
			Double vysledok = mc.calculateVolume(Double.valueOf(H.getText()), nadrz);
			result.setText("V nadrzi je: "+String.valueOf(vysledok)+" litrov");
			
			
			}catch(NumberFormatException err){
				System.out.print(err);
			JOptionPane.showMessageDialog(this, "Detekovane prazdne pole", "Doslo k chybe parsovania", JOptionPane.ERROR_MESSAGE);
			}
			
		}
		if(e.getSource().equals(bClear)){
			
			L.setText("");
			H.setText("");
			A.setText("");
			R.setText("");
			l.setText("");
			alpha.setText("");
		}
	
	}
	public void windowActivated(WindowEvent arg0) {
		
	}
	public void windowClosed(WindowEvent arg0) {
		
	}
	public void windowClosing(WindowEvent arg0) {
		
	}
	public void windowDeactivated(WindowEvent arg0) {
		
		
	}
	public void windowDeiconified(WindowEvent arg0) {

		
	}
	public void windowIconified(WindowEvent arg0) {

		
	}
	public void windowOpened(WindowEvent arg0) {

		
	}

}
class CanvasClass extends Canvas{
	public void paint(Graphics g){
		Graphics2D g2D = (Graphics2D)g;
		g2D.drawOval(200, 200, 50, 100);
		Image im = Toolkit.getDefaultToolkit().getImage("/home/andreus/aibb-nadrz/grafika/nadrz_poseidon.png");
		g2D.drawImage(im, 0, 0, this);
	}
	public void kresli(Graphics g){

		Graphics2D g2D = (Graphics2D)g;
		Image im = Toolkit.getDefaultToolkit().getImage("/media/junk/Skola/Tprj/nadrz.png");
		g2D.drawImage(im, 0, 0, this);
		g2D.drawLine(100, 100, 200, 200);
	}
}

class RoundedBorder implements Border {

    private int radius;
    private int width;
    private int height;

    RoundedBorder(int radius) {
        this.radius = radius;
    }
    RoundedBorder(int width, int height){
    	this.width=width;
    	this.height=height;
    }
    public Insets getBorderInsets(Component c) {
        return new Insets(this.radius+1, this.radius+1, this.radius+2, this.radius);
    }


    public boolean isBorderOpaque() {
        return true;
    }


    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
        g.drawRoundRect(x,y,width-1,height-1,radius,radius);
        //g.drawOval(x, y, width, height);
    }
}
class IntegerTextField extends JTextField {
	final static String badchars = "`~-!@#$%^&*()_+=\\|\"':;?/><, ";
	
public void processKeyEvent(KeyEvent ev) {
	char c = ev.getKeyChar();
	
	if((Character.isLetter(c) && !ev.isAltDown())  || badchars.indexOf(c) > -1) {
		ev.consume();
		return;
	}
	
	if(c == '-' && getDocument().getLength() > 0)
		ev.consume();
	else super.processKeyEvent(ev);
	}
}
